package com.bmf.module.examination.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bmf.enums.ParticipateTypeEnum;
import com.bmf.module.examination.entity.TestinfoParticipate;
import com.bmf.module.examination.mapper.TestinfoParticipateMapper;
import com.bmf.module.examination.service.TestinfoParticipateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * (TestinfoParticipate)表服务实现类
 *
 * @author makejava
 * @since 2023-02-03 14:59:46
 */
@Service("testinfoParticipateService")
public class TestinfoParticipateServiceImpl extends ServiceImpl<TestinfoParticipateMapper, TestinfoParticipate> implements TestinfoParticipateService {

    @Override
    public void removeByTestInfoId(Integer id) {
        LambdaQueryWrapper<TestinfoParticipate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestinfoParticipate::getTestinfoId, id);
        remove(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveList(List<Integer> participateIds,Integer testinfoId,Integer participateType) {
        LambdaQueryWrapper<TestinfoParticipate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestinfoParticipate::getTestinfoId, testinfoId);
        if(count(wrapper)>0){
            //删除所有
            this.removeByTestInfoId(testinfoId);
        }
        //添加
        if(CollUtil.isNotEmpty(participateIds)){
            List<TestinfoParticipate> collect = participateIds.stream().map(s -> new TestinfoParticipate(null, s, testinfoId, participateType)).collect(Collectors.toList());
            saveBatch(collect);
        }
    }

    @Override
    public List<Integer> getByOrganizationId(List<Integer> ids) {
        if(CollUtil.isNotEmpty(ids)){
            LambdaQueryWrapper<TestinfoParticipate> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TestinfoParticipate::getParticipateType, ParticipateTypeEnum.DEPARTMENT.getCode())
                    .in(TestinfoParticipate::getParticipateId, ids);
            List<TestinfoParticipate> list = list(wrapper);
            if(CollUtil.isNotEmpty(list)){
                return list.stream().map(TestinfoParticipate::getTestinfoId).collect(Collectors.toList());
            }
            return null;
        }
        return null;
    }

    @Override
    public List<Integer> getAppointTestIds(Integer id) {
        LambdaQueryWrapper<TestinfoParticipate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TestinfoParticipate::getParticipateType, ParticipateTypeEnum.APPOINT.getCode())
            .eq(TestinfoParticipate::getParticipateId, id);
        List<TestinfoParticipate> list = list(wrapper);
        if(CollUtil.isNotEmpty(list)){
            return list.stream().map(TestinfoParticipate::getTestinfoId).collect(Collectors.toList());
        }
        return null;
    }
}
